package com.sqx.modules.course.controller;

import com.sqx.common.utils.Result;
import com.sqx.modules.course.entity.CourseDetails;
import com.sqx.modules.course.entity.dto.BatchAddCourseDetailDto;
import com.sqx.modules.course.entity.dto.BatchAddCourseDetailsDto;
import com.sqx.modules.course.entity.dto.UpdateUrlDto;
import com.sqx.modules.course.service.CourseDetailsService;
import com.sqx.modules.course.util.FfmpegUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import ws.schild.jave.EncoderException;
import ws.schild.jave.info.MultimediaInfo;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@Api(value = "短剧视频信息", tags = {"短剧视频信息"})
@RequestMapping(value = "/courseDetails")
public class CourseDetailsController {
    @Autowired
    private CourseDetailsService courseDetailsService;

    @PostMapping("/insertCourseDetails")
    @ApiOperation("添加短剧视频信息")
    public Result insertCourseDetails(@RequestBody CourseDetails courseDetails) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        courseDetails.setCreateTime(sdf.format(new Date()));
        try {
            MultimediaInfo videoInfo = FfmpegUtil.getVideoInfo(courseDetails.getVideoUrl(), 1);
            if (ObjectUtils.isNotEmpty(videoInfo)) {
                int totalSeconds = (int) (videoInfo.getDuration() / 1000);
                int minutes = totalSeconds / 60;
                int seconds = totalSeconds % 60;
                DecimalFormat df = new DecimalFormat("00");
                courseDetails.setDuration(df.format(minutes) + ":" + df.format(seconds));
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

        courseDetailsService.insert(courseDetails);
        return Result.success();
    }

    @PostMapping("/updateCourseDetails")
    @ApiOperation("修改短剧视频信息")
    public Result updateCourseDetails(@RequestBody CourseDetails courseDetails) {
        courseDetailsService.updateCourseDetails(courseDetails);
        return Result.success();
    }

    @PostMapping("/deleteCourseDetails")
    @ApiOperation("删除短剧视频信息")
    public Result deleteCourseDetails(String ids) {
        courseDetailsService.deleteCourseDetails(ids);
        return Result.success();
    }

    /**
     * 获取视频时长
     * @return
     */
    @GetMapping("/getDuration")
    public Result getDuration() {
        courseDetailsService.aa();
        return Result.success();
    }

    /**
     * 批量添加视频
     * @param batchAddCourseDetailDto
     * @return
     */
    @PostMapping("/batchAddCourseDetail")
    public Result batchAddCourseDetail(@RequestBody BatchAddCourseDetailDto batchAddCourseDetailDto) throws Exception{
        courseDetailsService.batchAddCourseDetail(batchAddCourseDetailDto);
        return Result.success();
    }

    /**
     * 批量添加视频(服务器获取文件名)
     * @param batchAddCourseDetailsDto
     * @return
     */
    @PostMapping("/batchAddCourseDetails")
    public Result batchAddCourseDetails(@RequestBody@Valid BatchAddCourseDetailsDto batchAddCourseDetailsDto) throws Exception{
        return courseDetailsService.batchAddCourseDetails(batchAddCourseDetailsDto);
    }

    /**
     * 获取远程服务器问价夹下的视频文件名
     * @param fileName
     * @return
     */
    @GetMapping("/getFileResourceName")
    public Result getFileResourceName(String fileName) {
        return courseDetailsService.getFileResourceName(fileName);
    }

    /**
     * 获取目录文件夹下的所有图片名称
     * @param fileName
     * @return
     */
    @GetMapping("/getFileImgName")
    public Result getFileImgName(String fileName) {
        return courseDetailsService.getFileImgName(fileName);
    }


    /**
     * 表格导入
     * @param file
     * @return
     */
    @PostMapping("/uploadExcel")
    @ResponseBody
    public Result uploadExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        return courseDetailsService.uploadExcel(file);
    }

    /**
     * 修改剧集视频链接前缀
     * @return
     */
    @PostMapping("/updateUrl")
    public Result updateUrl(@RequestBody UpdateUrlDto updateUrlDto){
        return courseDetailsService.updateUrl(updateUrlDto);
    }

    /**
     * 替换短剧封面图
     * @return
     */
    @PostMapping("/updateTitleImg")
    public Result updateTitleImg(){
        return courseDetailsService.updateTitleImg();
    }

    /**
     * 替换剧集封面图
     * @return
     */
    @PostMapping("/updateDetailImg")
    public Result updateDetailImg(){
        return courseDetailsService.updateDetailImg();
    }

    /**
     * 取集数排序
     * @return
     */
    @PostMapping("/detailsSort")
    public Result detailsSort(){
        return courseDetailsService.detailsSort();
    }
}
